﻿using SqlSugar;

namespace WeDonekRpc.SqlSugar.Virtual
{
    internal class VirtualQueue : IVirtualQueue
    {
        protected SqlSugarProvider _DbClient;
        public VirtualQueue ( SqlSugarProvider client )
        {
            this._DbClient = client;
        }
        public bool IsNull => this._DbClient.Queues.Count == 0;


        public Task<int> SubmitAsync ( bool isTran = true )
        {
            return this._DbClient.SaveQueuesAsync(isTran);
        }
        public int Submit ( bool isTran = true )
        {
            return this._DbClient.SaveQueues(isTran);
        }
        public void Delete<IdentityId> ( string table, string col, IdentityId value )
        {
            string where = col + "=@id";
            this._DbClient.Deleteable<object>().AS(table).Where(where, new
            {
                id = value
            }).AddQueue();
        }
        public void Delete<IdentityId> ( string table, string col, IdentityId[] value )
        {
            string where = col + " in (@id)";
            this._DbClient.Deleteable<object>().AS(table).Where(where, new
            {
                id = value
            }).AddQueue();
        }
        public void Insert ( string table, Dictionary<string, object> values )
        {
            this._DbClient.Insertable(values).AS(table).AddQueue();
        }

        public void Insert ( string table, Func<object> values )
        {
            this._DbClient.InsertableByDynamic(values).AS(table).AddQueue();
        }
        public void Update ( string table, Dictionary<string, object> values, string identityId )
        {
            this._DbClient.Updateable(values).AS(table).WhereColumns(identityId).AddQueue();
        }
        public void Update ( string table, Dictionary<string, object> values, string[] identityIds )
        {
            this._DbClient.Updateable(values).AS(table).WhereColumns(identityIds).AddQueue();
        }
        public void Update ( string table, Func<object> values, string identityId )
        {
            this._DbClient.UpdateableByDynamic(values).AS(table).WhereColumns(identityId).AddQueue();
        }
        public void Update ( string table, Func<object> values, string[] identityId )
        {
            this._DbClient.UpdateableByDynamic(values).AS(table).WhereColumns(identityId).AddQueue();
        }
    }
}
